﻿using System;
using System.Drawing;
using System.Windows.Forms;
using CalcSharp.Properties;

namespace CalcSharp
{
    public partial class settings_editor : Form
    {
        public settings_editor()
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty((string)Settings.Default["ParentBackgoundImage"]))
            {
                this.prevbox.BackgroundImage = Image.FromFile((string)Settings.Default["ParentBackgoundImage"]);
            }
            Font f = (Font)Settings.Default["ConsoleFont"];
            this.ConsoleEmu.Text = f.Name + ", " + Math.Round(f.SizeInPoints, 0).ToString();
        }

        private void btn_browse_Click(object sender, EventArgs e)
        {
            openbgdialog.ShowDialog();
            this.f_bg.Text = openbgdialog.FileName;
            prevbox.BackgroundImage = Image.FromFile(openbgdialog.FileName);
            prevbox.BackgroundImageLayout = ImageLayout.Tile;
        }

        private void btn_default_Click(object sender, EventArgs e)
        {
            Settings.Default["ParentBackgoundImage"] = null;
            Settings.Default["ParentBackgroundMode"] = ImageLayout.Tile;
            Settings.Default["console_color"] = Color.Black;
            Settings.Default["console_bg"] = Color.SkyBlue;
        }

        private void btn_aply_Click(object sender, EventArgs e)
        {
            Settings.Default["ParentBackgoundImage"] = this.f_bg.Text;
            Settings.Default["ParentBackgroundMode"] = ImageLayout.Tile;
            if (MemSOpt0.Checked) Settings.Default["MemAutoSaveType"] = 0;
            else if (MemSOpt1.Checked) Settings.Default["MemAutoSaveType"] = 1;
            else Settings.Default["MemAutoSaveType"] = 2;
            Settings.Default.Save();
            Settings.Default.Reload();
            ((Parentform)this.MdiParent).LoadSettings();
            this.Close();
        }

        private void ColorChange(object sender, EventArgs e)
        {
            Button caller = (Button)sender;
            switch (caller.Name)
            {
                case "btn_ch_text":
                    dlg_selectcolor.Color = tb_textcol.BackColor;
                    if (dlg_selectcolor.ShowDialog() == DialogResult.OK) tb_textcol.BackColor = dlg_selectcolor.Color;
                    break;
                case "btn_ch_bg":
                    dlg_selectcolor.Color = tb_bgcol.BackColor;
                    if (dlg_selectcolor.ShowDialog() == DialogResult.OK) tb_bgcol.BackColor = dlg_selectcolor.Color;
                    break;
                case "btn_ch_ok":
                    dlg_selectcolor.Color = tb_textok.BackColor;
                    if (dlg_selectcolor.ShowDialog() == DialogResult.OK) tb_textok.BackColor = dlg_selectcolor.Color;
                    break;
                case "btn_ch_warn":
                    dlg_selectcolor.Color = tb_textwarn.BackColor;
                    if (dlg_selectcolor.ShowDialog() == DialogResult.OK) tb_textwarn.BackColor = dlg_selectcolor.Color;
                    break;
                case "btn_ch_err":
                    dlg_selectcolor.Color = tb_texterr.BackColor;
                    if (dlg_selectcolor.ShowDialog() == DialogResult.OK) tb_texterr.BackColor = dlg_selectcolor.Color;
                    break;
            }
        }

        private void SetConsoleFont_Click(object sender, EventArgs e)
        {
            if (SelectFont.ShowDialog() == DialogResult.OK)
            {
                this.ConsoleEmu.Clear();
                this.ConsoleEmu.Text = SelectFont.Font.Name + ", " + Math.Round(SelectFont.Font.SizeInPoints, 0).ToString();
                this.ConsoleEmu.Font = SelectFont.Font;
            }
        }
    }
}